package com.jamie.billmanage.mamager;

import java.sql.ResultSet;

import com.jamie.billmanage.Staff.Department;
import com.jamie.billmanage.Staff.People;
import com.jamie.billmanage.utils.DataOprtation;

public class InfoModify {
	private DataOprtation dataOprtation;

	public InfoModify(DataOprtation dataOprtation) {
		this.dataOprtation = dataOprtation;
	}

	public boolean modifyStaff(String name, People people) {
		try {
			String sql = "SELECT name FROM basicinfo WHERE name = '" + name
					+ "' UNION SELECT name FROM basicinfo WHERE name = '" + people.getName() + "'";
			ResultSet resultset = this.dataOprtation.execQuery(sql);
			if (resultset == null)
				return false;
			resultset.last();
			if (resultset.getRow() == 0)
				return false;
			else if (resultset.getRow() >= 2)
				people.setName(null);

			sql = "UPDATE `basicinfo` SET " + people.generateModifySQL() + " WHERE `name` = '" + name + "'";
//			System.out.println(sql);
			this.dataOprtation.execUpdate(sql);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	public boolean modifyStaff(int no, People people) {
		try {
			String sql = "SELECT no FROM basicinfo WHERE no = " + no
					+ " UNION SELECT name FROM basicinfo WHERE name = '" + people.getName() + "'";
			ResultSet resultset = this.dataOprtation.execQuery(sql);
			if (resultset == null)
				return false;
			resultset.last();
			if (resultset.getRow() == 0)
				return false;
			else if (resultset.getRow() >= 2)
				people.setName(null);
//			System.out.println(resultset.getString(1));
			else if (!resultset.getObject(1).getClass().isAssignableFrom(int.class))
				return false;

			sql = "UPDATE `basicinfo` SET " + people.generateModifySQL() + " WHERE `no` = " + no;
			this.dataOprtation.execUpdate(sql);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}
	
	
	
	
	public boolean modifyDepartment(int no,Department department) {
		try {
			String sql = "SELECT no FROM departmentinfo WHERE no = " + no
					+ " UNION SELECT name FROM departmentinfo WHERE name = '" + department.getName() + "'";
			ResultSet resultset = this.dataOprtation.execQuery(sql);
			if (resultset == null)
				return false;
			resultset.last();
			if (resultset.getRow() == 0)
				return false;
			else if (resultset.getRow() >= 2)
				department.setName(null);
//			System.out.println(resultset.getString(1));
//			System.out.println(resultset.getObject(1).getClass());
			else if (!resultset.getObject(1).getClass().isAssignableFrom(int.class))
				return false;

			sql = "UPDATE `departmentinfo` SET " + department.generateModifySQL() + " WHERE `no` = " + no;
			this.dataOprtation.execUpdate(sql);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}
	
	
	public boolean modifyDepartment(String name, Department department) {
		try {
			String sql = "SELECT name FROM departmentinfo WHERE name = '" + name
					+ "' UNION SELECT name FROM departmentinfo WHERE name = '" + department.getName() + "'";
			ResultSet resultset = this.dataOprtation.execQuery(sql);
			if (resultset == null)
				return false;
			resultset.last();
//			System.out.println(resultset.getRow());
			if (resultset.getRow() == 0)
				return false;
			else if (resultset.getRow() >= 2)
				department.setName(null);
			else if(resultset.getString(1).equals(department.getName()))
				department.setName(null);

			sql = "UPDATE `departmentinfo` SET " + department.generateModifySQL() + " WHERE `name` = '" + name + "'";
//			System.out.println(sql);
			this.dataOprtation.execUpdate(sql);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

}
